package com.ochnas.volunteersignup.service;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import com.ochnas.volunteersignup.domain.User;

public class UserServiceImpl extends JpaDaoSupport implements UserService {
	private static int defaultCharLen = 8;
	
	@Override
	public boolean activateUserAccount(String emailAddress, String activationKey) {
		
		String queryString = "select u from User u where u.emailAddress = ?1 AND u.activationKey = ?2";
		List resultList = getJpaTemplate().find(queryString, emailAddress, activationKey);
		if (resultList.isEmpty()) throw new NoResultException("Unable to activate " + emailAddress + ".");
		User user = (User) resultList.get(0);
		user.setActivated(true); 
		
		user = getJpaTemplate().merge(user);
		getJpaTemplate().persist(user);
		
		return true;
	}

	@Override
	public User createUserAccount(String emailAddress, String password,
			String firstName, String lastName, String contactPhoneNumber) {
		// check for unique email
		String queryString = "select user from User user where user.emailAddress = ?1";
		List resultList = getJpaTemplate().find(queryString, emailAddress);
		if (!resultList.isEmpty()) throw new NonUniqueResultException("E-mail address already exist.");
		
		User user = new User(emailAddress, password, firstName, lastName, contactPhoneNumber);
		String activationKey = RandomStringUtils.random(defaultCharLen, true, true);
		user.setActivationKey(activationKey);		
		user.setActivated(true); // default to true for Assignment #2
		getJpaTemplate().persist(user);
		return user;
	}

	@Override
	public User loginUserAccount(String emailAddress, String password) {
		String queryString = 
			"select u from User u where u.emailAddress = ?1";
		List resultList = getJpaTemplate().find(queryString, emailAddress);
		if (resultList.isEmpty()) throw new NoResultException("Email address does not exist.");
		queryString = 
			"select u from User u where u.emailAddress = ?1 AND u.password = ?2";
		resultList = getJpaTemplate().find(queryString, emailAddress, password);
		if (resultList.isEmpty()) throw new NoResultException("Email address and password do not match.");
		queryString = 
			"select u from User u where u.emailAddress = ?1 AND u.password = ?2 AND u.isActivated = ?3";
		resultList = getJpaTemplate().find(queryString, emailAddress, password, true);
		if (resultList.isEmpty()) throw new NoResultException("Unable to login " + emailAddress + ".");
		return (User) resultList.get(0);
	}

	@Override
	public User findUserAccount(Long userId) {
		return getJpaTemplate().find(User.class, userId);
	}
	
	@Override
	public User updateUserAccount(Long userId, String firstName, String lastName, String contactPhoneNumber) {
	
		User user = getJpaTemplate().find(User.class, userId);
				
		user.setFirstName(firstName);
		user.setLastName(lastName);
		user.setContactPhoneNumber(contactPhoneNumber);
		
		user = getJpaTemplate().merge(user);
		getJpaTemplate().persist(user);
		
		return user;
	}
	
	@Override
	public User updatePassword(Long userId, String password) {
		
		User user = getJpaTemplate().find(User.class, userId);
		
		user.setPassword(password);
		
		user = getJpaTemplate().merge(user);
		getJpaTemplate().persist(user);
		
		return user;
	}


}
